import java.util.ArrayList;
import java.util.concurrent.Semaphore;

public class Clients 
{
	ArrayList<Client> clients;
	static Clients rInstance = null;
	Semaphore allClientsSemaphore = new Semaphore(1);
	public static Clients getInstance()
	{
		if(rInstance == null)
		{
			rInstance = new Clients();
		}
		return rInstance;
	}
	
	Clients()
	{
		clients = new ArrayList<Client>();
	}
	
	public boolean findPlayer(String name)
	{
		for (Client client : clients) 
		{
			if(client.connectedPlayer != null)
			{
				if(name.equals(client.connectedPlayer.name))
				{
					return true;
				}
			}
		}
		return false;
	}
	
	public Player returnPlayer(String name)
	{
		Client client = returnClient(name);
		if(client == null)
		{
			return null;
		}
		return client.connectedPlayer;
	}
	
	public Client returnClient(String name)
	{
		for (Client client : clients) 
		{
			if(client.connectedPlayer != null)
			{
				if(name.equals(client.connectedPlayer.name))
				{
					return client;
				}
			}
		}
		return null;
	}

	public ArrayList<Client> getClients()
	{
		return clients;
	}
	
	public void debug()
	{
		System.err.println("=============DEBUG========================");
		System.err.println("PLAYERS_IN_GAME==========================================");
		for (Player pl : Players.getInstance().players) 
		{
			System.err.println("player:"+ pl.name);
		}
		System.err.println("CONNECTED_PLAYERS==========================================");
		for (Client cl : clients) 
		{
			System.err.println("player:"+ (cl.connectedPlayer == null ? "BRAK PLAYERA" : cl.connectedPlayer.name ));
			if(cl.connectedPlayer != null)
			{
				for (Player play : cl.connectedPlayer.nearlyPlayers)
				{
					System.err.println("WIDZI:"+ play.name );
				}
			}
		}
		System.err.println("=============DEBUG_END======================");
	}
}
